package com.ebt.m.proposal_v2.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.adapter.MainProductAdapter;
import com.ebt.m.proposal_v2.base.Constant;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.mvp.base.MVPCompatFragment;
import com.ebt.m.proposal_v2.mvp.contract.ProposalProductsContract;
import com.ebt.m.proposal_v2.mvp.presenter.MainProductPresenter;
import com.ebt.m.proposal_v2.mvp.presenter.ProposalProductsPresenter;
import com.ebt.m.utils.JsonUtil;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.view.RecyclerViewItemDivider;
import com.ebt.m.widget.EBTProgress;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 品牌产品
 *
 * @author archer.qi
 *         Created on 2017/5/27.
 */
public class FragmentProducts extends MVPCompatFragment<ProposalProductsPresenter> implements ProposalProductsContract.View {

    @BindView(R.id.list)
    RecyclerView mRecyclerView;
    @BindView(R.id.empty)
    View mEmptyView;
    @BindView(R.id.product_empty_content)
    TextView mEmptyContent;
    @BindView(R.id.progress)
    EBTProgress mProgress;
    @BindView(R.id.empty_container)
    LinearLayout mEmptyContentContainer;
    @BindView(R.id.no_net_container)
    RelativeLayout noNetContainer;
    @BindView(R.id.net_slow_container)
    RelativeLayout netSlowContainer;
    @BindView(R.id.load_refresh)
    TextView loadRefresh;
    @BindView(R.id.load_refresh_net)
    TextView loadRefreshNet;

    public static final String ARG_KEY = "Param";
    private ParamGetRisks mArg;
    private MainProductAdapter mAdapter;
    private ResponseBrand mBrandSelect; // 选择的品牌

    public static FragmentProducts getInstance(ParamGetRisks param) {
        FragmentProducts frg = new FragmentProducts();
        Bundle extras = new Bundle();
        extras.putSerializable(ARG_KEY, param);
        frg.setArguments(extras);
        return frg;
    }

    @Override
    protected ProposalProductsPresenter createPresenter() {
        return new ProposalProductsPresenter(getActivity().getApplicationContext(), this);
    }

    @Override
    protected int setContentResource() {
        return R.layout.fragment_main_products;
    }

    @Override
    protected void onFragmentCreate() {
        initArguments();
        initRecyclerView();
    }

    private void initRecyclerView() {
        mEmptyContent.setText("该品牌下暂无产品");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new RecyclerViewItemDivider(getActivity(), LinearLayoutManager.VERTICAL)
                .setAppearance(Constant.DIVIDER_SIZE, Constant.DIVIDER_COLOR_STRING));

        if (mPresenter.getCompanyType() != MainProductPresenter.COMPANY_TYPE_INS && mAdapter != null) {
            mAdapter.clearData();
        }
    }

    @Override
    protected void onFragmentDestroy() {

    }


    @Override
    public void bindData(List<ResponseMain> data) {
        mAdapter = new MainProductAdapter(getActivity(), data, mArg);
        mAdapter.setLabelMode(true);
        mAdapter.showBrandLabel(false);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void showToast(String msg) {
        UIHelper.makeToast(getActivity(), msg);
    }

    @Override
    public void initArguments() {
        Bundle arg = getArguments();
        if (arg != null) {
            mArg = (ParamGetRisks) arg.getSerializable(ARG_KEY);
        }
    }

    @Override
    public void setEmptyView(boolean isEmpty) {

    }

    @Override
    public void showProgress() {
        mProgress.setVisibility(View.VISIBLE);
    }

    @Override
    public void cancelProgress() {
        mProgress.setVisibility(View.GONE);
    }

    @Override
    public void showEmpty(boolean empty, boolean no_net, boolean net_slow) {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyContentContainer.setVisibility(empty ? View.VISIBLE : View.GONE);
        noNetContainer.setVisibility(no_net ? View.VISIBLE : View.GONE);
        netSlowContainer.setVisibility(net_slow ? View.VISIBLE : View.GONE);
    }

    @OnClick({R.id.load_refresh, R.id.load_refresh_net})
    public void onRefreshClicked(View v) {
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    @Override
    public ParamGetRisks getArgument() {
        return mArg;
    }

    @Override
    public ResponseBrand getBrandSelect() {
        return mBrandSelect;
    }

    /**
     * 选择品牌后调用
     *
     * @param data
     */
    public void onMenuBrandChanged(ResponseBrand data) {
        mBrandSelect = data;
    }
}
